using System.Collections.Specialized;
using System.ComponentModel;
using EnvDTE;
using Microsoft.Practices.RecipeFramework.Library.Converters;
using Microsoft.Practices.RecipeFramework.Library.Editors;
using Microsoft.Practices.WizardFramework;
using VSSDKAssist.Assets.Converters;
using VSSDKAssist.Recipes.Common.CustomPages;

namespace VSSDKAssist.Recipes.CreateToolOptionsPage
{
	/// <summary>
	/// Custom page for CreateToolOptionsPageCP recipe
	/// </summary>
	public partial class CreateToolOptionsPageCP1 : WizardPageBase
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="CreateToolOptionsPageCP1"/> class.
		/// </summary>
		public CreateToolOptionsPageCP1()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CreateToolOptionsPageCP1"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
		public CreateToolOptionsPageCP1(WizardForm wizard)
			: base(wizard)
		{
			InitializeComponent();
		}

		/// <summary>
		/// Gets or sets the destination folder.
		/// </summary>
		/// <value>The destination folder.</value>
		[RecipeArgument]
		public string DestinationFolder
		{
			get { return veDestinationFolder.Value.ToString(); }
			set
			{
				if(value != null)
				{
					veDestinationFolder.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the RESX project item.
		/// </summary>
		/// <value>The RESX project item.</value>
		[RecipeArgument]
		public ProjectItem ResxProjectItem
		{
			get { return (ProjectItem)veResxProjectItem.Value; }
			set
			{
				if(value != null)
				{
					veResxProjectItem.TextBox.Text = new ProjectItemConverter().ConvertTo(value, typeof(string)) as string;
				}
			}
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
					this.veDestinationFolder.BeginInit();
					this.veDestinationFolder.ValueType = typeof(string);
					base.Site.Container.Add(veDestinationFolder);
					this.veDestinationFolder.ConverterInstance = new CSharpIdentifierConverter();
					this.veDestinationFolder.EndInit();

					this.veResxProjectItem.BeginInit();
					this.veResxProjectItem.ValueType = typeof(ProjectItem);
					base.Site.Container.Add(veResxProjectItem);
					SolutionPickerEditor solutionPickerEditor = new SolutionPickerEditor();
					StringDictionary solutionPickerEditorAttributes = new StringDictionary();
					solutionPickerEditorAttributes.Add("UnboundReferenceType", "VSSDKAssist.Recipes.Common.References.ResxProjectItemReference, VSSDKAssist");
					solutionPickerEditor.Configure(solutionPickerEditorAttributes);
					this.veResxProjectItem.EditorInstance = solutionPickerEditor;
					this.veResxProjectItem.ConverterInstance = new ProjectItemConverter();
					this.veResxProjectItem.EndInit();
				}
			}
		}

		private void veDestinationFolder_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veDestinationFolder.ConverterInstance != null)
			{
				errorProvider.SetError(veDestinationFolder, string.Empty);

				if(veDestinationFolder.Value != null)
				{
					DictionaryService.SetValue("DestinationFolder", veDestinationFolder.Value.ToString());
				}
				else
				{
					DictionaryService.SetValue("DestinationFolder", null);
					errorProvider.SetError(veDestinationFolder, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("DestinationFolder", veDestinationFolder.Value.ToString());
			}
		}

		private void veResxProjectItem_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			ProjectItem resxProjectItem = (ProjectItem)veResxProjectItem.Value;
			DictionaryService.SetValue("ResxProjectItem", resxProjectItem);
		}
	}
}